<template lang="pug">
    .layout-nav
        .nav-left
            .title(@click="toHome")
                .icon-logo(:style='{ backgroundImage: `url("${logo}")` }')
                h1 {{title}}
        .nav-user
            el-dropdown(trigger='click', @command='handleCommand')
                .user-avatar
                    .user-name-abbr {{ userNameAbbr }}
                el-dropdown-menu(slot='dropdown')
                    el-dropdown-item(command='logOut')
                        i.yd-icon-switch-button
                        span 登出
</template>
<script>
import _ from 'lodash'
import {mapGetters} from 'vuex'
import {PROJECT_NAME_CN} from '@/app-config'

export default {
    name: 'TopBar',
    data() {
        return {}
    },
    computed: {
        ...mapGetters({
            userName: 'userName',
        }),
        userNameAbbr() {
            const {userName} = this
            return userName ? userName[0].toUpperCase() : ''
        },
        logo() {
            return this.$store.state.global.pageConfig.logo
                || ''
        },
        title() {
            return this.$store.state.global.pageConfig.name || PROJECT_NAME_CN
        },
    },
    methods: {
        handleCommand(command) {
            const url = _.get(window, 'location.href')
            // eslint-disable-next-line sonarjs/no-small-switch
            switch (command) {
                case 'logOut': {
                    this.$store.dispatch('Logout', url)
                    break
                }
                default: {
                    /***/
                }
            }
        },
        toHome() {
            this.$router.push({path: '/home'})
        },
    },
    mounted() {
        //
    },
}
</script>

<style lang="scss" scoped>
.layout-nav {
    z-index: 100;
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: $nav-height;
    padding-right: 16px;
    background: #fff;
    box-shadow: 0 0 1px rgba(31, 38, 76, 0.24), 0 2px 6px rgba(31, 38, 76, 0.06),
    0 8px 16px -2px rgba(31, 38, 76, 0.04);

    .nav-left {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }

    .arrow {
        font-size: 16px;
        margin-left: 10px;
    }

    .title {
        display: flex;
        align-items: center;
        padding-left: 24px;
        cursor: pointer;

        h1 {
            font-family: 'PingFang SC';
            font-size: 16px;
            font-style: normal;
            font-weight: 600;
            line-height: 22px;
            color: #1f264d;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    }

    .icon-logo {
        width: 24px;
        height: 24px;
        margin-right: 16px;
        background: no-repeat center/contain;
    }

    .user-avatar {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        overflow: hidden;
        cursor: pointer;
        background-color: #6080f0;
        border-radius: 50%;

        &::before {
            position: absolute;
            top: 20px;
            left: -10px;
            width: 30px;
            height: 30px;
            content: '';
            background: #899fef;
            border-radius: 50%;
            opacity: 0.58;
        }

        &::after {
            position: absolute;
            top: -15px;
            left: 20px;
            width: 30px;
            height: 30px;
            content: '';
            background-image: linear-gradient(180deg, rgba(190, 205, 248, 0.15) 0%, #899fef 100%);
            border-radius: 50%;
            opacity: 0.65;
        }
    }

    .user-name-abbr {
        position: relative;
        z-index: 100;
        font: 20px 'Rubik-Medium';
        color: #fff;
    }
}
</style>
